package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dao.DishDao;
import com.itheima.domain.Dish;
import com.itheima.domain.R;
import com.itheima.dto.DishDto;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    //@Autowired
    //private DishFlavorService dishFlavorService;

    //新增菜品功能
    @PostMapping
    public R add(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        return R.success("添加成功");
    }

    //菜品分页查询
    @GetMapping("/page")
    public R selectSize(Integer page, Integer pageSize, String name){
        Page<DishDto> pageInfo = dishService.findByPage(page,pageSize,name);
      return R.success(pageInfo);
    }

    //菜品删除
    /*@DeleteMapping
    public R delectById(Long id){
       return dishService.delectById(id);
    }*/

    //菜品修改回显功能
    @GetMapping("/{id}")
    public R dishById(@PathVariable Long id){
        DishDto dishDto = dishService.dishDtoById(id);
        return R.success(dishDto);
    }

    //菜品修改功能
    @PutMapping
    public R updaeDishDto(@RequestBody DishDto dishDto){
        dishService.updateDishDto(dishDto);
        return R.success("修改成功");
    }

    //根据条件查询对应的菜品数据
    @GetMapping("/list")
    public R findDishByCategoryID(Long categoryId){
        List<DishDto> list = dishService.findDishByCategoryID(categoryId);
        return R.success(list);
    }

    //菜品管理逻辑批量删除
    @DeleteMapping
    public R removeIds(Long[] ids){
        dishService.deleteDishByIds(ids);
        return R.success("删除成功");
    }

    //菜品批量停售
   @PostMapping("/status/0")
   public R modify(Long[] ids){
        R r = dishService.updateByStatus(ids);
        return r;
   }

   //菜品批量起售
    @PostMapping("/status/1")
    public R modify1(Long[] ids){
        R r = dishService.updateByStatus1(ids);
        return r;
    }
}
